using System.Collections;
using System.Reflection;
using System.Text;
using UnityEngine;

[AddComponentMenu("Sources/Utils/HUDFPS")]
public class HUDFPS : MonoBehaviour
{
	public GameObject PanelFPS;

	public UILabel lbFPS;

	public UILabel lbAnother;

	public static bool isActive;

	public bool updateColor = true;

	public bool allowDrag = true;

	public float frequency = 0.5f;

	public int nbDecimal = 1;

	private float accum;

	private int frames;

	private string sFPS = string.Empty;

	private GUIStyle style;

	private string maxFPS = "0.0";

	private string minFPS = "300.0";

	private string middleFPS = "0.0";

	private float updateTime = 5f;

	private ArrayList fpsArray;

	private float fps;

	private float sumFps;

	private int countFps;

	private bool isDevelopment;

	private Coroutine refFpsCor;

	private void Start()
	{
		if (Debug.isDebugBuild)
		{
			isDevelopment = true;
			isActive = true;
		}
		if (!isDevelopment)
		{
			base.gameObject.SetActive(false);
			return;
		}
		sumFps = 0f;
		countFps = 0;
		fpsArray = new ArrayList();
	}

	[Obfuscation(Exclude = true)]
	private void updateMidFPS()
	{
		float num = 0f;
		foreach (float item in fpsArray)
		{
			if (item > 0f)
			{
				num += item;
			}
		}
		middleFPS = (num / (float)fpsArray.Count).ToString("f" + Mathf.Clamp(nbDecimal, 0, 10));
	}

	private void Update()
	{
		if (!isDevelopment)
		{
			base.gameObject.SetActive(false);
			return;
		}
		if (isActive && !PanelFPS.activeSelf)
		{
			PanelFPS.SetActive(true);
			refFpsCor = StartCoroutine(FPS());
			ResetFPS();
			InvokeRepeating("updateMidFPS", 5f, updateTime);
		}
		if (!isActive && PanelFPS.activeSelf)
		{
			PanelFPS.SetActive(false);
			StopCoroutine(refFpsCor);
			CancelInvoke("updateMidFPS");
		}
		if (isActive)
		{
			accum += Time.timeScale / Time.deltaTime;
			frames++;
		}
	}

	private IEnumerator FPS()
	{
		while (true)
		{
			fps = accum / (float)frames;
			sumFps += fps;
			countFps++;
			if (fps > 0f)
			{
				fpsArray.Add(fps);
				if (fpsArray.Count > 320)
				{
					fpsArray.RemoveAt(0);
				}
			}
			if (fps > float.Parse(maxFPS))
			{
				maxFPS = fps.ToString("f" + Mathf.Clamp(nbDecimal, 0, 10));
			}
			if (fps < float.Parse(minFPS))
			{
				minFPS = fps.ToString("f" + Mathf.Clamp(nbDecimal, 0, 10));
			}
			sFPS = fps.ToString("f" + Mathf.Clamp(nbDecimal, 0, 10));
			accum = 0f;
			frames = 0;
			UpdateLabels();
			yield return new WaitForSeconds(frequency);
		}
	}

	public void ResetFPS()
	{
		BCWDebug.Log("ResetFPS");
		fpsArray.Clear();
		accum = 0f;
		countFps = 0;
		sumFps = 0f;
		frames = 0;
		maxFPS = "0";
		minFPS = "300";
		middleFPS = "0";
	}

	private void UpdateLabels()
	{
		Color color = ((fps >= 30f) ? Color.green : ((!(fps < 10f)) ? Color.yellow : Color.red));
		lbFPS.color = color;
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.Append("FPS: ");
		stringBuilder.Append(sFPS);
		lbFPS.text = stringBuilder.ToString();
		StringBuilder stringBuilder2 = new StringBuilder();
		stringBuilder2.Append("MID: ");
		stringBuilder2.Append(middleFPS);
		stringBuilder2.AppendLine(string.Empty);
		stringBuilder2.Append("MIN: ");
		stringBuilder2.Append(minFPS);
		stringBuilder2.AppendLine(string.Empty);
		stringBuilder2.Append("MAX: ");
		stringBuilder2.Append(maxFPS);
		stringBuilder2.AppendLine(string.Empty);
		stringBuilder2.Append("PING: ");
		stringBuilder2.Append(PhotonNetwork.GetPing());
		stringBuilder2.AppendLine(string.Empty);
		lbAnother.text = stringBuilder2.ToString();
	}

	private static string FloatToString(float value)
	{
		return value.ToString("f1");
	}
}
